package com.have.blog.web.service.impl;

import com.have.blog.base.wrapper.Wrapper;
import com.have.blog.model.mapper.AdminMapper;
import com.have.blog.model.models.Admin;
import com.have.blog.model.models.AdminExample;
import com.have.blog.picture.service.PictureFeignApi;
import com.have.blog.utils.MyBeanUtils;
import com.have.blog.web.dto.AdminDto;
import com.have.blog.web.service.AdminService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Service
@SuppressWarnings("all")
@Slf4j
public class AdminServiceImpl implements AdminService {
    @Autowired
    private AdminMapper adminMapper;
    @Autowired
    private PictureFeignApi pictureFeignApi;


    @Override
    public AdminDto getAdminByAdminName(String adminName) {
        AdminDto result = new AdminDto();
        AdminExample example = new AdminExample();
        example.createCriteria()
                .andUsernameEqualTo(adminName);
        List<Admin> admins = adminMapper.selectByExampleWithBLOBs(example);
        if (CollectionUtils.isEmpty(admins)) {
            log.error("网站拥有者为空");
            return result;
        }
        Admin admin = admins.get(0);
        MyBeanUtils.copyProperties(admin, result);

        if (result.getHeadUid() != null) {
            Wrapper<String> wrapper = pictureFeignApi.getPictureUrl(result.getHeadUid());
            if (wrapper.isSuccess()) {
                String picUrl = wrapper.getData();
                result.setPhotoUrl(picUrl);
            }
        }
        return result;
    }
}
